package com.cwt_taxi_xa;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.cwt_taxi_xa.constants.Constants;
import com.cwt_taxi_xa.constants.SPConstants;
import com.cwt_taxi_xa.protocol.DeviceBiz;
import com.cwt_taxi_xa.protocol.DispatchCenterUsers;
import com.cwt_taxi_xa.service.DvrService;
import com.cwt_taxi_xa.util.SPUtils;
import com.cwt_taxi_xa.util.StringUtil;
import com.cwt_taxi_xa.util.ToastUtils;

/**
 * 行业通道参数设置
 *
 * @author wlh
 * @discrib
 *
 */
public class IndustryChannelSetting extends CWTBaseActivity implements OnFocusChangeListener
		,OnTouchListener{
	private static final String TAG = "IndustryChannelSetting";
	public EditText hytdsim,car_number;
	public EditText hytdip, hytdport;
	public EditText hytdipBak, hytdportBak;
	public RadioGroup radiogroup1,radiogroup2;
	public RadioButton rb_on, rb_off,rb_on_Bak, rb_off_Bak;
	String hytdsimStr,hytdipStr,hytdportStr,carNumber;
	String hytdipbakStr,hytdportbakStr;
	int hytdswitch;
	int hytdswitchBak;
	//	private Button btn_register;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.industry_channel_setting);

		setupViews();
		initData();
		addListener();

	}

	private void setupViews() {
		// TODO Auto-generated method stub
		hytdsim = (EditText) findViewById(R.id.industry_sim);
		car_number = (EditText) findViewById(R.id.car_number);
		//		hytdapn = (EditText) findViewById(R.id.industry_apn);
		hytdip = (EditText) findViewById(R.id.industry_ip);
		hytdport = (EditText) findViewById(R.id.industry_port);

		hytdipBak = (EditText) findViewById(R.id.industry_ip_bak);
		hytdportBak = (EditText) findViewById(R.id.industry_port_bak);

		radiogroup1 = (RadioGroup) findViewById(R.id.radiogroup1);
		rb_on = (RadioButton) findViewById(R.id.rb_on);
		rb_off = (RadioButton) findViewById(R.id.rb_off);

		radiogroup2 = (RadioGroup) findViewById(R.id.radiogroup2);
		rb_on_Bak = (RadioButton) findViewById(R.id.rb_on_bak);
		rb_off_Bak = (RadioButton) findViewById(R.id.rb_off_bak);

		//		btn_register = (Button) findViewById(R.id.btn_register);
	}

	private void addListener() {
		// TODO Auto-generated method stub
		hytdsim.setOnFocusChangeListener(this);
		car_number.setOnFocusChangeListener(this);
		hytdip.setOnFocusChangeListener(this);
		hytdport.setOnFocusChangeListener(this);

		hytdipBak.setOnFocusChangeListener(this);
		hytdportBak.setOnFocusChangeListener(this);
		/*btn_register.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//				boolean hasRegis = SPutil.getTerminalRegist().getBoolean("regit_flag", false);
				//				if(hasRegis){
				//					ToastUtils.showToast("设备已注册，无须重复注册！");
				//				}else{
				startActivity(new Intent(IndustryChannelSetting.this, RegisterActivity.class));
				//			}
			}
		});*/
		radiogroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup radiogroup, int i) {
				switch (i) {
					case R.id.rb_on:
						DeviceBiz.setSwitcher(1);
						break;
					case R.id.rb_off:
						DeviceBiz.setSwitcher(0);
						break;
				}
				DispatchCenterUsers.stopComm(6);
			}
		});

		radiogroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup radiogroup, int i) {
				switch (i) {
					case R.id.rb_on_bak:
						DeviceBiz.setSwitcherBak(1);
						break;
					case R.id.rb_off_bak:
						DeviceBiz.setSwitcherBak(0);
						break;
				}
				//DispatchCenterUsers.stopComm(6);
			}
		});
	}

	/**
	 * 初始化数据
	 */
	public void initData(){
		hytdipStr = DeviceBiz.getIp();
		hytdportStr = DeviceBiz.getPort();
		hytdswitch = DeviceBiz.getSwitcher();

		hytdsimStr = DeviceBiz.getDeviceCode();

		hytdipbakStr = DeviceBiz.getIpBak();
		hytdportbakStr = DeviceBiz.getPortBak();
		hytdswitchBak = DeviceBiz.getSwitcherBak();
		carNumber = DeviceBiz.getCarNumberSingleLetter();

		//设置默认弹出数字键盘
		//hytdip.setInputType(EditorInfo.TYPE_CLASS_PHONE);

		switch (hytdswitch) {
			case 0:
				rb_on.setChecked(false);
				rb_off.setChecked(true);
				break;
			case 1:
				rb_on.setChecked(true);
				rb_off.setChecked(false);
				break;
			default:
				break;
		}
		hytdip.setText(hytdipStr);
		hytdport.setText(hytdportStr);
		hytdsim.setText(hytdsimStr);
		car_number.setText(carNumber);

		switch (hytdswitchBak) {
			case 0:
				rb_on_Bak.setChecked(false);
				rb_off_Bak.setChecked(true);
				break;
			case 1:
				rb_on_Bak.setChecked(true);
				rb_off_Bak.setChecked(false);
				break;
			default:
				break;
		}
		hytdipBak.setText(hytdipbakStr);
		hytdportBak.setText(hytdportbakStr);

	}
	/**
	 * 保存行业中心sim
	 */
	public void saveHytdsim(){
		String simNumber = hytdsim.getText().toString().trim();
//		if(!simNumber.equals(hytdsimStr)){
		if(simNumber.length() == 12){
			DeviceBiz.setDeviceCode(simNumber);
			DispatchCenterUsers.stopComm(6);
			if(SPUtils.getParamInteger(SPConstants.SPK_dvr, SPConstants.SPV_dvr)==1){
				DvrService.getInstance().sendSet(simNumber.substring(2), car_number.getText().toString().trim());
			}else{
				DvrService.getInstance().sendHttpGetSet();
			}
		}else{
			ToastUtils.showToastForCustom("请输入正确的终端ID");
		}
//		}
	}
	/**
	 * 保存车牌号
	 */
	public void saveCarNumber(){
		String carNumberNew = car_number.getText().toString().trim();
		if(!carNumberNew.equals(carNumber)){
			if(carNumberNew.length() == 6 || carNumberNew.length() == 7){
				DeviceBiz.setCarNumber(carNumberNew);
				DeviceBiz.setCarNumberCorrection(true);
				DispatchCenterUsers.stopComm(6);
			}else{
				ToastUtils.showToastForCustom("请输入正确的车牌号");
			}
		}
	}

	/**
	 * 保存行业中心ip
	 */
	public void saveHytdip(){
		String ip = hytdip.getText().toString().trim();
		if(!StringUtil.isNull(ip)){
			if(!ip.equals(hytdipStr)){
				//获取第一位
				if(StringUtil.isNumeric(ip.substring(0, 1))){
					if(StringUtil.checkIP(ip) || !Constants.PORT_PROTECT){
						DeviceBiz.setIp(ip);
						DispatchCenterUsers.stopComm(6);
					}else{
						ToastUtils.showToastForCustom("请输入正确的IP格式");
					}
				}else{
					if(StringUtil.checkWebSite("http://"+ip)){
						DeviceBiz.setIp(ip);
						DispatchCenterUsers.stopComm(6);
					}else{
						ToastUtils.showToastForCustom("请输入正确的域名格式");
					}
				}
			}
		}
	}
	/**
	 * 保存行业中心端口
	 */
	public void saveHytdport(){
		if(hytdport.getText()!=null){
			if(!hytdport.getText().toString().equals(hytdportStr)){
				DeviceBiz.setPort(hytdport.getText().toString());
				DispatchCenterUsers.stopComm(6);
			}
		}
	}

	/**
	 * 保存行业中心ip
	 */
	public void saveHytdBakIp(){
		if(hytdipBak.getText()!=null){
			if(!hytdipBak.getText().toString().equals(hytdipbakStr)){
				//获取第一位
				if(StringUtil.isNumeric(hytdipBak.getText().toString().substring(0, 1))){
					if(StringUtil.checkIP(hytdipBak.getText().toString()) || !Constants.PORT_PROTECT){
						DeviceBiz.setIpBak(hytdipBak.getText().toString());
						//DispatchCenterUsers.stopComm(6);

					}else{
						ToastUtils.showToastForCustom("请输入正确的IP格式");
					}
				}else{
					if(StringUtil.checkWebSite("http://"+hytdipBak.getText().toString())){
						DeviceBiz.setIpBak(hytdipBak.getText().toString());
						//DispatchCenterUsers.stopComm(6);

					}else{
						ToastUtils.showToastForCustom("请输入正确的域名格式");
					}
				}
			}
		}
	}
	/**
	 * 保存行业中心端口
	 */
	public void saveHytdBakPort(){
		if(hytdportBak.getText()!=null){
			if(!hytdportBak.getText().toString().equals(hytdportbakStr)){
				DeviceBiz.setPortBak(hytdportBak.getText().toString());
				DispatchCenterUsers.stopComm(6);
			}
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			saveHytdip();
			saveHytdport();
			saveHytdBakIp();
			saveHytdBakPort();
			IndustryChannelSetting.this.finish();
		} else if (keyCode == KeyEvent.KEYCODE_MENU) {
			openOptionsMenu();
		}
		return true;
	}

	/**
	 * 失去焦点事件
	 */
	@Override
	public void onFocusChange(View view, boolean flag) {
		switch (view.getId()) {
			case R.id.industry_port:
				if (flag == false) {
					saveHytdport();
				}
				break;
			case R.id.industry_ip:
				if (flag == false) {
					saveHytdip();
				}
				break;
			case R.id.industry_sim:
				if (flag == false) {
					saveHytdsim();
				}
				break;
			case R.id.car_number:
				if (flag == false) {
					saveCarNumber();
				}
				break;
			case R.id.industry_port_bak:
				if (flag == false) {
					saveHytdBakPort();
				}
				break;
			case R.id.industry_ip_bak:
				if (flag == false) {
					saveHytdBakIp();
				}
				break;
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		saveHytdip();
		saveHytdport();
		saveHytdBakIp();
		saveHytdBakPort();
		saveHytdsim();
		saveCarNumber();
		setConnectBak();
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		return false;
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	public void setStatus(int index, boolean status) {
		// TODO Auto-generated method stub

	}

	@Override
	public void sendMsg(int type, Object obj) {
		// TODO Auto-generated method stub

	}

	//
	private void setConnectBak(){
		DispatchCenterUsers.socketManager.connect();
	}
}
